package GUI.System;

import java.awt.Color;

/**
 * Panel for when a customer has forgotten their password.
 */
public class ForgotPassPanel extends javax.swing.JPanel {

    private GUIFrame frame;
    private int errors;

    /**
     * Custom constructor.
     */
    public ForgotPassPanel() {
        initComponents();
        errors = 1;
    }

    /**
     * Setter for GUIFrame reference.
     *
     * @param frame GUIFrame object.
     */
    public void setFrame(GUI.System.GUIFrame frame) {
        this.frame = frame;
    }

    /**
     * Check input fields for errors.
     */
    private void inputCheck() {
        if (!textEmail.getText().equals("")) {
            if (isValidEmailAddress(textEmail.getText()) == true) {
                labEmail.setForeground(Color.BLACK);
                labErrorEmail.setVisible(false);
                errors--;
            } else {
                labEmail.setForeground(Color.RED);
                labErrorEmail.setVisible(true);
            }
        } else {
            labEmail.setForeground(Color.RED);
            labErrorEmail.setVisible(true);
        }
    }

    /**
     * Method found at StackOverflow.com, which validates the syntax of an email
     * address.
     * http://stackoverflow.com/questions/624581/what-is-the-best-java-email
     * -address-validation-method
     *
     * @param email
     *
     * @return true if the string matches the structure of a valid email
     *         address; false oStherwise.
     */
    private boolean isValidEmailAddress(String email) {
        java.util.regex.Pattern p = java.util.regex.Pattern.compile(
                ".+@.+\\.[a-z]+");
        java.util.regex.Matcher m = p.matcher(email);
        return m.matches();
    }

    /**
     * Reset fields.
     */
    private void resetPage() {
        labEmail.setForeground(Color.BLACK);
        labErrorEmail.setVisible(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labRecover = new javax.swing.JLabel();
        labEmail = new javax.swing.JLabel();
        labInformation1 = new javax.swing.JLabel();
        textEmail = new javax.swing.JTextField();
        btnBack = new javax.swing.JButton();
        labErrorEmail = new javax.swing.JLabel();
        btnSubmit = new javax.swing.JButton();
        labInformation2 = new javax.swing.JLabel();

        labRecover.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        labRecover.setText("Recover Password");

        labEmail.setText("Email");

        labInformation1.setText("Enter your email to recover your password. The email specified must be ");

        btnBack.setText("Back");
        btnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            }
        });

        labErrorEmail.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        labErrorEmail.setForeground(new java.awt.Color(255, 0, 0));
        labErrorEmail.setText("- not a valid email adress");

        btnSubmit.setText("Submit");
        btnSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSubmitActionPerformed(evt);
            }
        });

        labInformation2.setText("that of a valid account registered in our system.");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labInformation1)
                    .addComponent(labRecover)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnBack)
                        .addGap(18, 18, 18)
                        .addComponent(btnSubmit))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labEmail)
                        .addGap(18, 18, 18)
                        .addComponent(labErrorEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(textEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labInformation2, javax.swing.GroupLayout.PREFERRED_SIZE, 357, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addComponent(labRecover)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labInformation1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labInformation2)
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labEmail)
                    .addComponent(labErrorEmail))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(textEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnBack)
                    .addComponent(btnSubmit))
                .addContainerGap(70, Short.MAX_VALUE))
        );

        labErrorEmail.setVisible(false);
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Change to login panel.
     *
     * @param evt ActionEvent
     */
    private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackActionPerformed
        frame.changePanel("card1");
        resetPage();
    }//GEN-LAST:event_btnBackActionPerformed

    /**
     * If no errors in input fields, change to panel for password found.
     *
     * @param evt ActionEvent
     */
    private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSubmitActionPerformed
        inputCheck();
        if (errors == 0) {
            frame.cManager.setLoggedInUser(frame.cManager.getCustomerByEmail(
                    textEmail.getText().toLowerCase()));
            frame.changePanel("card7");
        }
    }//GEN-LAST:event_btnSubmitActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBack;
    private javax.swing.JButton btnSubmit;
    private javax.swing.JLabel labEmail;
    private javax.swing.JLabel labErrorEmail;
    private javax.swing.JLabel labInformation1;
    private javax.swing.JLabel labInformation2;
    private javax.swing.JLabel labRecover;
    private javax.swing.JTextField textEmail;
    // End of variables declaration//GEN-END:variables
}
